<template>
  <el-dialog
    v-model="dialogFormVisible"
    destroy-on-close
    :title="title"
    width="800px"
    @close="close"
  >
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="业务类型名称" prop="BusinessClassName">
        <el-input v-model="form.BusinessClassName" style="width: 200px" />
      </el-form-item>
      <el-form-item label="流程设置">
        <el-checkbox :checked="true" label="到单录入" size="large" />
        <el-checkbox
          v-model="form.isPayment"
          :checked="form.isPayment"
          label="收款"
          size="large"
        />
        <el-checkbox
          v-model="form.isVoicing"
          :checked="form.isVoicing"
          label="开票"
          size="large"
        />
        <el-checkbox
          v-model="form.isSubmitOrder"
          :checked="form.isSubmitOrder"
          label="销售提单"
          size="large"
        />
        <el-checkbox
          v-model="form.isSaleReview"
          :checked="form.isSaleReview"
          label="销售内勤审核"
          size="large"
        />
        <el-checkbox
          v-model="form.isOperReview"
          :checked="form.isOperReview"
          label="运营内勤审核"
          size="large"
        />
        <el-checkbox
          v-model="form.isAssignOrder"
          :checked="form.isAssignOrder"
          label="分单"
          size="large"
        />
      </el-form-item>
      <el-form-item label="要求上传合同">
        <el-checkbox
          v-model="form.isContract"
          :checked="form.isContract"
          label="上传合同"
          size="large"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/systemsetBusinessClass'

  export default defineComponent({
    name: 'SystemsetBusinessClassEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        form: {
          roles: [],
        },
        rules: {
          title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        },
        title: '',
        dialogFormVisible: false,
      })

      const showEdit = (row) => {
        if (!row) {
          state.title = '添加'
        } else {
          state.title = '编辑'
          state.form = Object.assign({}, row)
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          roles: [],
        }
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>
